#include "main.h"
#include "global.h"


/* Gestion de la souris */
int ancienx, ancieny, tournerXY=0, translaterXY=0, bougerZ=0;


/* Boutons de la souris */
void mouse( int button, int state, int x, int y )
{
  /* enfoncer gauche */
  if( button==GLUT_LEFT_BUTTON && state==GLUT_DOWN ){
    tournerXY = 1;
    ancienx = x;
    ancieny = y;
  }
  /* relacher gauche */
  else if( button==GLUT_LEFT_BUTTON && state==GLUT_UP ){
    tournerXY = 0;
  }
  /* enfoncer milieu */
  if( button==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN ){
    bougerZ = 1;
    ancienx = x;
    ancieny = y;
  }
  /* relacher milieu */
  else if( button==GLUT_MIDDLE_BUTTON && state==GLUT_UP ){
    bougerZ = 0;
  }
  /* enfoncer droit */
  else if( button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN ){
    translaterXY = 1;
    ancienx = x;
    ancieny = y;
  }
  /* relacher droit */
  else if( button==GLUT_RIGHT_BUTTON && state==GLUT_UP ){
    translaterXY = 0;
  }
}


/* Changement de position de la souris */
void mouseMotion( int x, int y )
{
	double dx,dy,nrm;

	if( tournerXY || translaterXY || bougerZ )
	{
		/* deplacement */
		dx = x - ancienx;
		dy = ancieny - y; /* axe vertical dans l'autre sens */
		
		if( tournerXY ){
			nrm = sqrt( dx*dx+dy*dy+dx*dx+dy*dy );
			glLoadIdentity();
			glRotatef( nrm, -dy, dx, 0 );/*axe perpendiculaire au deplacement*/
			glMultMatrixf( tb_rotation );
			glGetFloatv( GL_MODELVIEW_MATRIX, tb_rotation );
		}
		else if( translaterXY ){
			tb_translation[0] += dx/100.0;
			tb_translation[1] += dy/100.0;
		}
		else {
			tb_translation[2] += dy/100.0;
			glLoadIdentity();
			glRotatef( dx, 0,0,-1 );/*axe perpendiculaire a l'ecran*/
			glMultMatrixf( tb_rotation );
			glGetFloatv( GL_MODELVIEW_MATRIX, tb_rotation );
		}
		ancienx = x;
		ancieny = y;
		display();
	}
}

